/**
 * Created by yunpeng on 2014/12/8.
 */
var Table = require('../utils/commonDBConn').Table;
var debug = require('debug')('serviceType');

var ServiceType = function(serv_name, describe) {
    this.serv_name = serv_name;//服务名称
    this.describe = describe;//服务描述

    if (typeof this.save != 'function') {//仅在第一次调用的初始化
        ServiceType.prototype.save = function(){
            var sql = 'insert into citys set ?';
            //var args = {username:this.username,nickname:this.nickname,password:this.password,usertype:this.usertype,integral:this.integral,active_id:this.active_id,did:this.did,createtime:this.createtime};
            db.execQuery(sql,this,function(result){
                console.log(result);
            });
        };
    }
    if (typeof this.getServiceTypeList!='function'){
        ServiceType.prototype.getServiceTypeList = function(callback){
            new Table('service_type').queryAll(function(err,result){
                if(err){
                    callback(null);
                }else{
                    callback(result);
                }
            })
        }

        ServiceType.prototype.list = function(option, callback){
            var column = '*';
            var condition1 = '';
            var condition2 = [option.sort, option.order];
            var page = Number(option.page);
            var rows = Number(option.rows);
            condition2.push((page-1) * rows);
            condition2.push(rows);
            new Table('service_type l').queryPage(column, condition1, condition2, function(err, data){
                if(!err){
                    callback(data);
                }else{
                    debug(err);
                    callback({
                        err:'err',
                        total:0,
                        rows:[]
                    })
                }
            })
        }
        ServiceType.prototype.edit = function(option, callback){
            option.serv_id = Number(option.serv_id);
            var ids = 'serv_id';
            new Table('service_type').update(ids, option, function(err, data){
                if(!err){
                    if(data.affectedRows > 0){
                        callback({state:'success'})
                    }else{
                        callback({state:'fail'})
                    }
                }else{
                    callback({err:'err'})
                }
            })
        }
        ServiceType.prototype.add = function(option, callback){
            new Table('service_type').insert(option, function(err, data){
                if(!err){
                    if(data.affectedRows > 0){
                        callback({state:'success'})
                    }else{
                        callback({state:'fail'})
                    }
                }else{
                    callback({err:'err'})
                }
            })
        }
        ServiceType.prototype.del = function(ids, callback){
            var id = 'serv_id';
            new Table('service_type').delete(id, ids, function(err, data){
                if(!err){
                    callback({count:data.affectedRows})
                }else{
                    callback({err:'err'})
                }
            })
        }
        ServiceType.prototype.predel = function(ids, callback){
            var cond = 'and stypeid in(' + ids + ')';
            new Table('life_service').count(cond, function(err, data){
                if(!err){
                    callback({count:data});
                }else{
                    callback({err:'err'})
                }
            })
        }
    }
}
exports.ServiceType = ServiceType;